package com.ruoyi.mp.controller;

import com.ruoyi.common.core.constant.MsgConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.db.domain.system.MpCashApply;
import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.PcTeamConfig;
import com.ruoyi.db.domain.system.dto.MpClientFriendCount;
import com.ruoyi.db.domain.web.dto.MpRegistForm;
import com.ruoyi.mp.annotion.Login;
import com.ruoyi.mp.annotion.LoginUser;
import com.ruoyi.mp.service.IMpClientLoginService;
import com.ruoyi.mp.service.IMpClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 【商城用户】Controller
 *
 * @author ruoyi
 * @date 2022-07-22
 */
@Api(value = "【商城用户】",tags ="【商城用户】")
@RestController
@RequestMapping("/smp/client")
public class MpClientController extends BaseController
{
    private static Logger log = LoggerFactory.getLogger(MpClientController.class);
    @Autowired
    private IMpClientService mpClientService;
    @Autowired
    private IMpClientLoginService mpClientLoginService;


    /**
     * "根据手机号获取用户信息
     */
    @Login
    @ApiOperation(value = "根据手机号获取用户信息")
    @GetMapping("/getInfo/mobile")
    public AjaxResult getInfoByMobile(String mobile,@LoginUser MpClient client)
    {
        try {
            if(StringUtils.isBlank(mobile)){
                return AjaxResult.error("手机号不能为空");
            }
            Pattern p = Pattern.compile("^1[3-9]\\d{9}$");
            Matcher m = p.matcher(mobile);
            if(!m.matches()){
                return AjaxResult.error("手机号格式不正确");
            }
            if(mobile.equals(client.getMobile())){
                return AjaxResult.error("不能输入自己的手机号");
            }
            MpClient clientT = new MpClient();
            clientT.setMobile(mobile);
            List<MpClient> list = mpClientService.selectMpClientList(clientT);
            return AjaxResult.success(list.get(0));
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
        }
        return AjaxResult.error("【商城用户】异常");
    }

    /**
     * 获取【商城用户】详细信息
     */
    @Login
    @ApiOperation(value = "获取【商城用户】详细信息")
    @ApiImplicitParam(name="clientId",value="【商城用户】")
    @GetMapping
    public AjaxResult getInfo(@LoginUser MpClient client)
    {
        try {
            MpClient client1 = mpClientService.selectMpClientAndParentByClientId(client);
            return AjaxResult.success(client1);
        }catch (Exception e){
            log.error("【商城用户】发生异常",e);
        }
        return AjaxResult.error("【商城用户】异常");
    }


    /**
     * 修改【商城用户】
     */
    @Login
    @ApiOperation(value = "修改【商城用户】")
    @Log(title = "【商城用户】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MpClient mpClient,@LoginUser MpClient client)
    {
        try {
            mpClient.setClientId(client.getClientId());
            int i=mpClientService.updateMpClient(mpClient);
            return toAjax(i);
        }catch (Exception e){
            log.error("修改【商城用户】发生异常",e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
     * 发送短信
     */
    @Login
    @ApiOperation(value = "用户修改交易密码发送短信")
    @GetMapping ("/payPassword/sendSms")
    public R updPayPwdSendSms (@LoginUser MpClient client)
    {
        String mobile = client.getMobile();
        Boolean isSend = mpClientLoginService.sendSmsByMobile(mobile,3);
        if(isSend){
            return R.ok(null,MsgConstants.SMS_SUCCESS_SEND);
        }
        return R.fail("短信发送失败");
    }
    /**
     * 修改交易密码
     */
    @Login
    @ApiOperation(value = "修改交易密码")
    @PostMapping("/payPassword/upd")
    public R updPay (@RequestBody MpRegistForm form, @LoginUser MpClient client)
    {
        mpClientService.updPayPassword(form,client);
        return R.ok();
    }
    /**
     * 修改账号密码
     */
    @Login
    @ApiOperation(value = "修改账号密码")
    @PostMapping("/password/upd")
    public R updPassword (@RequestBody MpRegistForm form, @LoginUser MpClient client)
    {
        mpClientService.updPassword(form,client);
        return R.ok();
    }
    /**
     * 发送短信
     */
    @Login
    @ApiOperation(value = "用户修改交易密码发送短信")
    @GetMapping ("/password/sendSms")
    public R updPwdSendSms (@LoginUser MpClient client)
    {
        String mobile = client.getMobile();
        Boolean isSend = mpClientLoginService.sendSmsByMobile(mobile,5);
        if(isSend){
            return R.ok(null,MsgConstants.SMS_SUCCESS_SEND);
        }
        return R.fail("短信发送失败");
    }
    /**
     * 修改账号密码
     */
    @Login
    @ApiOperation(value = "修改手机号")
    @PostMapping("/mobile/upd")
    public R updMobile (@RequestBody MpRegistForm form, @LoginUser MpClient client)
    {
        mpClientService.updMobile(form,client);
        return R.ok();
    }
    @Login
    @ApiOperation(value = "用户修改手机号发送短信")
    @GetMapping ("/mobile/sendSms")
    public R updMobileSendSms (@LoginUser MpClient client)
    {
        String mobile = client.getMobile();
        Boolean isSend = mpClientLoginService.sendSmsByMobile(mobile,6);
        if(isSend){
            return R.ok(null,MsgConstants.SMS_SUCCESS_SEND);
        }
        return R.fail("短信发送失败");
    }
    /**
      * 发送短信
     */
    @Login
    @ApiOperation(value = "用户绑定银行卡发送短信")
    @GetMapping("/bindBank/sendSms")
    public R bindBankSendSms (@LoginUser MpClient client)
    {
        String mobile = client.getMobile();
        Boolean isSend = mpClientLoginService.sendSmsByMobile(mobile,4);
        if(isSend){
            return R.ok(null,MsgConstants.SMS_SUCCESS_SEND);
        }
        return R.fail("短信发送失败");
    }
    /**
     * 发送短信
     */
    @Login
    @ApiOperation(value = "获取用户收益统计")
    @GetMapping("/earns/report")
    public R getEarnsReport (@LoginUser MpClient client)
    {
        Map<String,Object> map = mpClientLoginService.getEarnsReport(client);
        return R.ok(map);
    }
    /**
     * 发送短信
     */
    @Login
    @ApiOperation(value = "获取一级二级好友收益")
    @GetMapping("/earns/myFriend")
    public TableDataInfo getEarnsMyFriend (Integer type,@LoginUser MpClient client,Integer pageNum,Integer pageSize)
    {
        List<MpClient> list = mpClientLoginService.getEarnsMyFriend(client,type,pageNum,pageSize);
        return getDataTable(list);
    }
    @Login
    @ApiOperation(value = "是否有店铺")
    @GetMapping("/isHavTeam")
    public R geTeam (@LoginUser MpClient client)
    {
        PcTeamConfig team = mpClientLoginService.getTeam(client);
        return R.ok(team);
    }



}
